<template>
  <div class="container">
    <van-field v-model="message" rows="2" autosize type="textarea" maxlength="50" placeholder="请输入热评" show-word-limit />
    <van-button type="default" size="mini" round @click="onPost">发布</van-button>
  </div>
</template>
<script>
import { addComment } from '@/api/comment.js'
export default {
  props: {
    target: {
      type: [Object, String, Number],
      ruquired: true
    },
    articleId: {
      type: [Object, String, Number],
      default: null
    }
  },
  data() {
    return {
      message: ''
    }
  },
  methods: {
    async onPost() {
      if (this.message.trim().length === 0) {
        return this.$toast({ message: '请输入热评' })
      }
      this.$toast.loading({ message: '发布中...', forbidClick: true })
      const { data: res } = await addComment({
        target: this.target.toString(),
        art_id: this.articleId ? this.articleId.toString() : null,
        content: this.message
      })
      this.$emit('post-success', res.data.new_obj)
      this.message = ''
      this.$toast.success('评论成功！')
    }
  }
}
</script>
<style lang="less" scoped>
.container {
  display: flex;
  align-items: center;
  padding: 5px 14px;
  .van-button {
    width: 50px;
  }
  .van-field {
    background-color: #eee;
    margin-right: 5px;
  }
}
</style>
